package cn.smbms.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import cn.smbms.dao.ProviderMapper;
import cn.smbms.dao.UserMapper;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.utils.MyBatisUtil;

public class UserService {
	public static final String PAGEPATH = "/WEB-INF/jsp/";

	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String opr = request.getParameter("opr");
		if ("show".equals(opr)) {
			String userCode = request.getParameter("userCode");
			request.setAttribute("userCode", userCode);
			request.getRequestDispatcher(PAGEPATH + "login.jsp").forward(request, response);
			return;
		} else if ("login".equals(opr)) {
			String userCode = request.getParameter("userCode");
			String userPassword = request.getParameter("userPassword");
			SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
			User user = sqlSession.getMapper(UserMapper.class).login(userCode, userPassword);
			MyBatisUtil.closeSqlSession(sqlSession);
			if (user != null) {
				request.getSession().setAttribute("USER", user);
				request.getRequestDispatcher(PAGEPATH + "welcome.jsp").forward(request, response);
				return;
			} else {
				response.getWriter().print("<script>alert('用户名与密码错误！');location.href='user-login.do?opr=show&userCode="
						+ userCode + "'</script>");

				return;
			}
		} else if ("loginOut".equals(opr)) {
			request.getSession().invalidate();
			response.sendRedirect("user-login.do?opr=show");
			return;
		}
	}

	public void updatePwd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String opr = request.getParameter("opr");
		if ("show".equals(opr)) {
			request.getRequestDispatcher(PAGEPATH + "password.jsp").forward(request, response);
			return;
		} else if ("updatePwd".equals(opr)) {
			User user = (User) request.getSession().getAttribute("USER");
			Integer id = user.getId();
			String password = request.getParameter("password");
			SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
			int i = sqlSession.getMapper(UserMapper.class).updatePwd(id, password);
			MyBatisUtil.closeSqlSession(sqlSession);
			if (i > 0) {
				response.getWriter()
						.print("<script>alert('修改密码成功！');location.href='user-updatePwd.do?opr=showWelcome'</script>");
				return;
			} else {
				response.getWriter()
						.print("<script>alert('修改密码失败！');location.href='user-updatePwd.do?opr=show'</script>");
				return;
			}
		} else if ("showWelcome".equals(opr)) {
			request.getRequestDispatcher(PAGEPATH + "welcome.jsp").forward(request, response);
			return;
		}

	}

	public void getProviderList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		String proName = request.getParameter("proName");
		List<Provider> providerList = sqlSession.getMapper(ProviderMapper.class).getAllByproName(proName);
		MyBatisUtil.closeSqlSession(sqlSession);
		request.setAttribute("providerList", providerList);
		request.setAttribute("proName", proName);
		request.getRequestDispatcher(PAGEPATH + "providerList.jsp").forward(request, response);

	}

	public void getProviderById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		Integer providerId = null;
		try {
			providerId = Integer.parseInt(request.getParameter("providerId"));
		} catch (NumberFormatException e) {
		}
		Provider provider = sqlSession.getMapper(ProviderMapper.class).getProviderById(providerId);
		MyBatisUtil.closeSqlSession(sqlSession);
		request.setAttribute("provider", provider);
		String opr = request.getParameter("opr");
		request.getRequestDispatcher(PAGEPATH + "provider" + opr + ".jsp").forward(request, response);

	}

	public void addProvider(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		String opr = request.getParameter("opr");
		if (opr.equals("show")) {
			request.getRequestDispatcher(PAGEPATH + "providerAdd.jsp").forward(request, response);
			return;
		} else if (opr.equals("add")) {
			String proCode = request.getParameter("proCode");
			String proName = request.getParameter("proName");
			String proContact = request.getParameter("proContact");
			String proPhone = request.getParameter("proPhone");
			String proAddress = request.getParameter("proAddress");
			String proFax = request.getParameter("proFax");
			String proDesc = request.getParameter("proDesc");
			Provider p = new Provider(proCode, proName, proDesc, proContact, proPhone, proAddress, proFax);
			int i = sqlSession.getMapper(ProviderMapper.class).addProvider(p);
			MyBatisUtil.closeSqlSession(sqlSession);
			response.getWriter().print("<script>alert('账单添加" + (i == 1 ? "成功" : "失败")
					+ "！');location.href='provider-getProviderList.do';</script>");

		}

	}

	public void updateProvider(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		Integer id = null;
		try {
			id = Integer.parseInt(request.getParameter("providerId"));
		} catch (NumberFormatException e) {
		}
		String proCode = request.getParameter("proCode");
		String proName = request.getParameter("proName");
		String proContact = request.getParameter("proContact");
		String proPhone = request.getParameter("proPhone");
		String proAddress = request.getParameter("proAddress");
		String proFax = request.getParameter("proFax");
		String proDesc = request.getParameter("proDesc");
		Provider p = new Provider(id, proCode, proName, proDesc, proContact, proPhone, proAddress, proFax);
		int i = sqlSession.getMapper(ProviderMapper.class).updateProvider(p);
		MyBatisUtil.closeSqlSession(sqlSession);

		response.getWriter().print("<script>alert('账单修改" + (i == 1 ? "成功" : "失败")
				+ "！');location.href='provider-getProviderList.do';</script>");

	}

	public void deleteProvider(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		Integer providerId = null;
		try {
			providerId = Integer.parseInt(request.getParameter("providerId"));
		} catch (NumberFormatException e) {
		}
		int i = sqlSession.getMapper(ProviderMapper.class).deleteProvider(providerId);
		// int i = 1;
		MyBatisUtil.closeSqlSession(sqlSession);
		response.getWriter().print("<script>alert('账单删除" + (i == 1 ? "成功" : "失败")
				+ "！');location.href='provider-getProviderList.do';</script>");

	}
}
